package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.UserAddress;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.addressform.InsertAddressForm;
import com.example.wechattakeaway.form.addressform.UpdateAddressForm;
import com.example.wechattakeaway.mapper.UserAddressMapper;
import com.example.wechattakeaway.service.AddressService;
import com.example.wechattakeaway.utils.PageUtil;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Repository
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    UserAddressMapper userAddressMapper;

    @Override
    public ResultVO InsertAddress(InsertAddressForm insertAddressForm) {
        UserAddress userAddress = new UserAddress();
        userAddress.setSysuserId(insertAddressForm.getSysuserId());
        userAddress.setacceptAddress(insertAddressForm.getAcceptAddress());
        userAddress.setAcceptPhone(insertAddressForm.getAcceptPhone());
        userAddress.setSex(insertAddressForm.getSex());
        userAddress.setSysuserName(insertAddressForm.getSysuserName());
        userAddress.setLabel(insertAddressForm.getLabel());

        try{
            userAddressMapper.InsertAddress(userAddress);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(userAddress);
    }

    @Override
    public ResultVO UpdateAddress(UpdateAddressForm updateAddressForm) {
        try{
            userAddressMapper.UpdateAddress(updateAddressForm);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO DeleteAddress(Integer addressId) {
        try{
            userAddressMapper.DeleteAddress(addressId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO SelectAddressById(Integer userId) {
        UserAddress userAddress = new UserAddress();
        try{
            userAddress = userAddressMapper.SelectAddressById(userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(userAddress);
    }

    @Override
    public ResultVO SelectAddressByUser(Integer pageNum,Integer sysuserId) {
        List<UserAddress> userAddressList = new ArrayList<>();
        try{
            PageHelper.startPage(pageNum,5);
            userAddressList = userAddressMapper.SelectAddressByUser(sysuserId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap = PageUtil.getMap(userAddressList);
        return ResultVOUtil.success(hashMap);
    }

}
